/*
   CashDrawer.cpp
   
   Copyright (C) 2006 LinuxOne, Inc. <Republic of korea>

   This library is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1
   of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Lessor General Public License for more details.

   You should have received a copy of the GNU Lessor General Public
   License along with this library; if not, write to the Free Software
   Foundation, inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

   Hyunjin Kim <mgc521@linuxone.co.kr>

   LinuxOne, Inc., hereby disclaims all copylight
   written by Hyunjin Kim, 20 December 2006
*/

#include <services/CashDrawerService.h>

namespace booyopos
{

namespace services
{

CCashDrawerService::CCashDrawerService()
{
	deviceServiceDescription = "BooyoPOS CashDrawer Device Service";
	deviceServiceVersion = 0;
	physicalDeviceDescription = "Valcretec CashDrawer Device";
	physicalDeviceName = "VCB-355S";

	capStatus=false;
	capPowerReporting=PR_NONE;
	capStatusMultiDrawerDetect=false;
	capStatisticsReporting=false;
	capUpdateStatistics=false;
	capCompareFirmwareVersion=false;
	capUpdateFirmware=false;
	drawerOpened=false;
	powerNotify=PN_DISABLED;
	powerState=PS_UNKNOWN;
}

CCashDrawerService::~CCashDrawerService()
{
}

/* Capabilities */
bool
CCashDrawerService::GetCapStatus()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return capStatus;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

int
CCashDrawerService::GetCapPowerReporting()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return capPowerReporting;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

bool
CCashDrawerService::GetCapStatusMultiDrawerDetect()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return capStatusMultiDrawerDetect;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

bool
CCashDrawerService::GetCapStatisticsReporting()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return capStatisticsReporting;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

bool
CCashDrawerService::GetCapUpdateStatistics()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return capUpdateStatistics;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

bool
CCashDrawerService::GetCapCompareFirmwareVersion()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return capCompareFirmwareVersion;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

bool
CCashDrawerService::GetCapUpdateFirmware()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return capUpdateFirmware;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

/* Properties */
bool
CCashDrawerService::GetDrawerOpened()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return drawerOpened;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

int
CCashDrawerService::GetPowerNotify()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return powerNotify;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

void
CCashDrawerService::SetPowerNotify(int powerNotify)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		CCashDrawerService::powerNotify = powerNotify;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

int
CCashDrawerService::GetPowerState()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		return powerState;
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

/* Methods */
void
CCashDrawerService::OpenDrawer()
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}
		// Open CashDrawer
		//char buf[5]={};

		char buf[5] = {};

		buf[0] = 0x1B;
		buf[1] = 0x70;
		buf[2] = 0x00;
		buf[3] = 0x0F;
		buf[4] = 0x0F;

		int len;
		len = write(fd, buf, 5); 

		if(len)
		{
			drawerOpened=1;
		}
		else
		{
			throw len;
		}
	}
	catch(CByException bye)
	{
		throw bye;
	}
	catch(int len)
	{
		std::cout << "OpenDrawer is not opend" << std::endl;
	}
}

void
CCashDrawerService::WaitForDrawerClose(int beepTimeout, int beepFrequency, int beepDuration, int beepDelay)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		/* TO DO */
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

/* Statistics */
void
CCashDrawerService::ResetStatistics(std::string statisticsBuffer)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		/* TO DO */
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

void
CCashDrawerService::RetrieveStatistics(std::string statisticsBuffer[])
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		/* TO DO */
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

void
CCashDrawerService::UpdateStatistics(std::string statisticsBuffer)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		/* TO DO */
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

void
CCashDrawerService::CompareFirmwareVersion(std::string firmwareFileName, int* result)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		/* TO DO */
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

void
CCashDrawerService::UpdateFirmware(std::string firmwareFileName)
throw (CByException)
{
	try
	{
		if(!bOpen)
		{
			throw CByException(E_CLOSED, "Device is not opened");
		}

		/* TO DO  */
	}
	catch(CByException bye)
	{
		throw bye;
	}
}

} // namespace services

} // namespace booyopos
